package com.useeinfo.asg.open.system;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.AddressBiz;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.asg.modules.security.biz.SystemRoleBiz;
import com.useeinfo.asg.modules.security.biz.SystemUserBiz;
import com.useeinfo.asg.modules.security.entity.SystemRole;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.xiaoleilu.hutool.util.CollectionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 宝玉 on 2017/8/23.
 */
@Controller
@RequestMapping("/admin/system/user")
public class SystemUserAdminManageAction {

    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private SystemRoleBiz systemRoleBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/system/systemUser/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(HttpServletRequest request,
                               @RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "sidx", required = false) String sidxParam,
                               @RequestParam(value = "sord", required = false) String sordParam) {

        String userAccount = CommUtil.null2String(request.getParameter("searchUserAccount"));
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        if(!userAccount.trim().equals("")) {
            queryParam.getSqlMap().put("userAccount", userAccount.trim());
        }
        return systemUserBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, ModelMap mv) {

        long id = CommUtil.null2Long(request.getParameter("id"));
        String roleSet = "";
        if(id > 0) {
            SystemUser user = systemUserBiz.findModel(id);
            mv.put("user", user);
            Set<SystemRole> roles = user.getSystemRoleSet();
            if(roles != null) {
                for(SystemRole role : roles) {
                    roleSet += "-" + role.getId();
                }
                roleSet += "-";
            }
        }
        mv.put("roleSet", roleSet);
        // 获取系统角色列表，用于给系统用户分配角色
        List<SystemRole> roleList = systemRoleBiz.findList();
        mv.put("roleList", roleList);

        return "/system/systemUser/edit";
    }

    @RequestMapping("/addOrEdit")
    @ResponseBody
    public JSONObject addOrEdit(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        String userAccount = CommUtil.null2String(request.getParameter("userAccount"));
        String userName = CommUtil.null2String(request.getParameter("userName"));
        String[] roleNames = request.getParameterValues("roleName");
        SystemUser user = null;
        if(id < 0 || (user = systemUserBiz.findModel(id)) == null) {
            user = new SystemUser();
            user.setCreateTime(new Date());
        }
        user.setUserAccount(userAccount);
        user.setUserName(userName);
        //判断用户名是否重复
        if (systemUserBiz.existUserAccount(user)) {
            retJson.put("msg", "您输入的用户名已存在，请重新输入");
            return retJson;
        }
        // 角色分配
        Set<SystemRole> roleSet = new HashSet<SystemRole>();
        if(roleNames != null && roleNames.length > 0) {
            for (String roleId : roleNames) {
                roleSet.add(systemRoleBiz.findModel(CommUtil.null2Long(roleId)));
            }
        }
        user.setSystemRoleSet(roleSet);
        systemUserBiz.addOrUpdate(user);
        retJson = CommUtil.jsonResult(true);

        return retJson;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        SystemUser user = null;
        if(id < 0 || (user = systemUserBiz.findModel(id)) == null) {
            retJson.put("msg", "未找到数据源");
            return retJson;
        }
        user.setDeleteStatus(true);
        systemUserBiz.update(user);
        return retJson;
    }

    @RequestMapping("/resetPassword")
    @ResponseBody
    public JSONObject resetPassword(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        SystemUser user = null;
        if(id < 0 || (user = systemUserBiz.findModel(id)) == null) {
            retJson.put("msg", "未找到数据源");
            return retJson;
        }
        user.setUserPassword("123456");
        systemUserBiz.update(user);
        return retJson;
    }
}
